package com.ss.android.article.base.app;

import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.ttnet.a.AppConfig;
import com.bytedance.ttnet.b.RequestParams;
import com.ss.android.common.applog.AppLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.zip.GZIPOutputStream;

public final class RequestValidator {
    private static String FEED_PATH = null;

    public static void a() {
        try {
            RequestValidator.FEED_PATH = URI.create("http://ib.snssdk.com/api/news/feed/v46/").getPath();
        } catch (Throwable throwable) {
        }
        com.bytedance.ttnet.utils.a.a(new com.bytedance.ttnet.utils.a.a$a());
    }

    public static void a(String uri, String arg11, JSONObject resHeaders, JSONArray hijackCellIndexes) {
        GZIPOutputStream gzipOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream;
        if (!android.support.design.a.isTextEmpty(arg11)) {
            try {
                byte[] bytes = arg11.getBytes();
                int length = arg11.length();
                JSONObject jo = new JSONObject();
                jo.put("uri", uri);
                jo.put("timestamp", System.currentTimeMillis() / 1000);
                if (hijackCellIndexes != null && hijackCellIndexes.length() > 0) {
                    jo.put("hijack_cell_indexes", hijackCellIndexes);
                }

                if (resHeaders != null) {
                    jo.put("res_headers", resHeaders);
                }

                byteArrayOutputStream = new ByteArrayOutputStream(32768);
                gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
                gzipOutputStream.write(bytes, 0, length);
                byte[] bytes2 = byteArrayOutputStream.toByteArray();
                int length2 = bytes2.length;
                jo.put("response", com.bytedance.common.utility.aa.a(bytes2, Math.min(32768, length2)));
                AppLog.b("ss_sign_sample", jo.toString());
            } catch (Throwable throwable) {
                Logger.w("RequestValidator", "compress with gzip exception: " + throwable);
            } finally {
                if (gzipOutputStream != null) {
                    try {
                        gzipOutputStream.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    }

    public static boolean isFeed(String fullUrl) {
        return !android.support.design.a.isTextEmpty(RequestValidator.FEED_PATH)
                && !android.support.design.a.isTextEmpty(fullUrl)
                && fullUrl.indexOf(RequestValidator.FEED_PATH) > 0;
    }

    public static boolean checkSign(String arg7, String arg8, String arg9, RequestParams requestContext) {
        if ((android.support.design.a.isTextEmpty(arg7)) || (android.support.design.a.isTextEmpty(arg9))) {
            return false;
        } else if (!AppConfig.i()) {
        } else if (android.support.design.a.isTextEmpty(RequestValidator.FEED_PATH)) {
        } else if (arg7.indexOf(RequestValidator.FEED_PATH) > 0) {
            String local_sign = com.bytedance.common.utility.aa.md5("ByteDance" + com.bytedance.common.utility.aa.md5(arg9));
            if (!TextUtils.isEmpty(local_sign) && local_sign.length() == 32) {
                String ss_sign = "";
                long decodeStartTs = System.currentTimeMillis();
                if (!TextUtils.isEmpty(arg8)) {
                    try {
                        ss_sign = android.support.design.a.decode(arg8);
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                    }
                }
                long decodeEndTs = System.currentTimeMillis();
                requestContext.ss_sign = ss_sign;
                requestContext.local_sign = local_sign;
                requestContext.body_is_json = RequestValidator.isJson(arg9);
                requestContext.decodeTime = decodeEndTs - decodeStartTs;
                return local_sign.equals(ss_sign);
            }
            Logger.w("RequestValidator", "bad checksum " + local_sign);
        }
        return true;
    }

    public static boolean validContent(JSONObject jo) {
        if (jo != null) {
            String code = jo.optString("code");
            String contentMd5 = com.bytedance.common.utility.aa.md5(jo.optString("content"));
            try {
                String md5 = android.support.design.a.decode(code);
                if (contentMd5 == null) {
                    return false;
                }
                return contentMd5.equals(md5);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        return false;
    }

    private static boolean isJson(String str) {
        boolean v0 = true;
        try {
            new JSONObject(str);
        } catch (JSONException v1) {
            try {
                new JSONArray(str);
            } catch (JSONException v0_1) {
                v0 = false;
            }
        }

        return v0;
    }
}

